from django import template
from django.shortcuts import reverse
from django.forms.models import ModelChoiceField
register = template.Library()


@register.inclusion_tag("content.html")
def form_tag(form):
    for i in form:
        """
        from django.forms.boundfield import BoundField
        print("model_form",type(i))
        model_form <class 'django.forms.boundfield.BoundField'>
        model_form <class 'django.forms.boundfield.BoundField'>
        model_form <class 'django.forms.boundfield.BoundField'>
        model_form <class 'django.forms.boundfield.BoundField'>
        model_form <class 'django.forms.boundfield.BoundField'>
        print(i.field)
        <django.forms.fields.CharField object at 0x000000000402CE80>
        <django.forms.fields.CharField object at 0x000000000402CEF0>
        <django.forms.fields.TypedChoiceField object at 0x000000000402CF60>
        <django.forms.models.ModelMultipleChoiceField object at 0x000000000402CFD0>
        <django.forms.models.ModelChoiceField object at 0x000000000403A080>
        """
        if isinstance(i.field, ModelChoiceField):
            # 为form对象增加一个可以判断是否是 单选或者多选的属性
            i.is_pop = True
            _url = "%s_%s_add" % (i.field.queryset.model._meta.app_label, i.field.queryset.model._meta.model_name)
            i.url = reverse(_url) + "?pop_id=id_%s" % i.name
    return {"form":form}